//package com.hmall.common.aop;
//
//import cn.hutool.core.util.ObjUtil;
//import com.hmall.common.utils.UserContext;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.springframework.amqp.core.MessagePostProcessor;
//import org.springframework.stereotype.Component;
//
//import java.util.Arrays;
//
//@Aspect
//@Component
//@Slf4j
//public class RabbitHeaderAspect {
//
//    @Around("execution(* org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(..))")
//    public Object addLoginInfoToHeaders(ProceedingJoinPoint joinPoint) throws Throwable {
//        Object[] args = joinPoint.getArgs();
//
//        MessagePostProcessor enhancer = message -> {
//            Long userId = UserContext.getUser();
//            log.debug("RabbitHeaderAspect - 注入 user-info header: {}", userId);
//            if (ObjUtil.isNotNull(userId)) {
//                message.getMessageProperties().setHeader("user-info", userId);
//            }
//            return message;
//        };
//
//        if (args.length == 4 && args[3] instanceof MessagePostProcessor) {
//            MessagePostProcessor original = (MessagePostProcessor) args[3];
//            args[3] = (MessagePostProcessor) message ->
//                    enhancer.postProcessMessage(original.postProcessMessage(message));
//            return joinPoint.proceed(args);
//        } else if (args.length == 4 && args[3] == null) {
//            args[3] = enhancer;
//            return joinPoint.proceed(args);
//        } else if (args.length == 3) {
//            args = Arrays.copyOf(args, 4);
//            args[3] = enhancer;
//            return joinPoint.proceed(args);
//        }
//
//        return joinPoint.proceed();
//    }
//}
